package com.huluobo.a2203parkdemo.login

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2203parkdemo.MainActivity
import com.huluobo.a2203parkdemo.R
import com.huluobo.a2203parkdemo.databinding.ActivityLoginBinding
import com.huluobo.base.bean.LoginBean
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

@AndroidEntryPoint
class LoginActivity : BaseActivity<ActivityLoginBinding, LoginViewModel>() {
    override fun initData() {
        viewModel.onLoginSuccess.observe(this) {
            val loginBean: LoginBean = Gson().fromJson(it, LoginBean::class.java)
            ToastUtils.showLong("欢迎回来:${loginBean.uName}")
            SPUtils.getInstance().put(Const.PARAM_TOKEN, loginBean.token)
            SPUtils.getInstance().put(Const.PARAM_USERNAME, loginBean.uName)
            SPUtils.getInstance().put(Const.PARAM_PASSWORD, loginBean.uPwd)
            SPUtils.getInstance().put(Const.PARAM_USER_ID, loginBean.uId)
            viewModel.insertUser(loginBean)
            SPUtils.getInstance().put("isLogin", false)
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
            finish()
        }

        viewModel.insertUserComplete.observe(this) {
            Log.i("TagA", "数据库:$it")
        }
    }

    override fun initView() {
        if (SPUtils.getInstance().getString(Const.PARAM_USERNAME).isNotEmpty() && SPUtils.getInstance()
                .getString(Const.PARAM_PASSWORD).isNotEmpty()
        ) {
            val map = mutableMapOf<String, String>()
            map[Const.PARAM_USERNAME] = SPUtils.getInstance().getString(Const.PARAM_USERNAME)
            map[Const.PARAM_PASSWORD] = SPUtils.getInstance()
                .getString(Const.PARAM_PASSWORD)
            map[Const.PARAM_TIME] = (System.currentTimeMillis() / 1000).toString()

            val json = Gson().toJson(map)
            val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
            SPUtils.getInstance().put("isLogin", true)
            viewModel.login(body)
        }

        binding.loginBtn.setOnClickListener {
            val username = binding.loginUsernameEt.text.toString()
            val password = binding.loginPasswordEt.text.toString()
            val timeString = (System.currentTimeMillis() / 1000).toString()

            val map = mutableMapOf<String, String>()
            map[Const.PARAM_USERNAME] = username
            map[Const.PARAM_PASSWORD] = password
            map[Const.PARAM_TIME] = timeString

            val json = Gson().toJson(map)
            val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
            SPUtils.getInstance().put("isLogin", true)
            viewModel.login(body)
        }

        binding.loginHeader.headerBack.visibility = View.GONE
    }
}